var mongoose = require('mongoose');

var dbhost = 'localhost';
//var dbhost = '64.23.72.68';
//var dbhost = '172.28.60.160';
var dbname = 'fog';

var gridCollectionName = 'opengrid';
var timeCollectionName = 'timepoint';

var db = mongoose.createConnection(dbhost, dbname);

//handle db connection event
db.on('error', console.error.bind(console, 'connection error.'));
db.on('open', function() {
	console.log('connected to ' + dbname + ' database on ' + dbhost);
});

var GridSchema = new mongoose.Schema({
	_id : String,
	gridx : Number,
	gridy : Number,
	prop : {
		writime : Number,
		color : String,
		class : String,
		//subject : String,
		keyword : String,
		key_id: String,
		content : String,
		//good : Number,
		//bad : Number,
		opacity : Number
	},
	relaArrays : {
		nearBy : [Boolean],
		relaIn : [Boolean],
		relaOut : [Boolean],
		nearbyG : []
	}
});

var GridModel = db.model(gridCollectionName, GridSchema);
var GridData = db.model(gridCollectionName);

var TpSchema = new mongoose.Schema({
	grid_id : String,
	timepoint : Number,
	level : Number
});

var TpModel = db.model(timeCollectionName, TpSchema);
var TpData = db.model(timeCollectionName);

exports.insertTimepoint = function(id, point) {
	var data = new TpData();
	data.grid_id = id;
	data.timepoint = point;
	
	console.log(data);
	data.save();
};

exports.GridModel = GridModel;
exports.GridData = GridData;

exports.TpModel = TpModel;
exports.TpData = TpData;

